home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gnulib / libsrc98.zoo / fcntl.c < prev    next >
C/C++ Source or Header  |  1993-03-01  |  1KB  |  68 lines

  1. /*
  2.  * limited emulation of fcntl
  3.  *
  4.  * ++jrb    bammi@cadence.com
  5.  */
  6.  
  7. #include <fcntl.h>
  8. #include <stdarg.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include "lib.h"
  12.  
  13. #ifdef __STDC__
  14. int fcntl (int f, int cmd, ...)
  15. #else
  16. int fcntl (f, cmd)
  17. int f;
  18. int cmd;
  19. #endif
  20. {
  21.     int handle;
  22.     va_list argp;
  23.  
  24.     va_start(argp, cmd);
  25.  
  26.     /* check for valid descriptor */
  27.     handle = __OPEN_INDEX(f);
  28.     if( (handle >= __NHANDLES) || (handle < 0) )
  29.     {
  30.     errno = EBADF;
  31.     return -1;
  32.     }
  33.     
  34.     if(__open_stat[handle].status == FH_UNKNOWN)
  35.     isatty(f); /* try to set .status */
  36.  
  37.     if((__open_stat[handle].status == FH_UNKNOWN) ||
  38.       ((__open_stat[handle].status == FH_ISAFILE) && (__open_stat[handle].filename == (char *)0)) )
  39.     {
  40.     errno = EBADF;
  41.     return -1;
  42.     }
  43.  
  44.     switch(cmd) {
  45.       case F_DUPFD:
  46.     /* there is no way to emulate this even reasonably under TOS,  so... */
  47.     return dup(f);
  48.  
  49.       case F_GETFD:
  50.     return (int) (__open_stat[handle].eclose);
  51.  
  52.       case F_SETFD:
  53.     __open_stat[handle].eclose = va_arg(argp, int) & 1;
  54.     return 0;
  55.  
  56.       case F_GETFL: /* fudge city */
  57.     return (int)(*((short *)(&(__open_stat[handle]))));
  58.  
  59.       case F_SETFL:
  60.     *((short *)(&(__open_stat[handle]))) = va_arg(argp, int);
  61.     return 0;
  62.  
  63.       default:
  64.     errno = EBADARG;
  65.     return -1;
  66.     }
  67. }
  68.